{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "442e39ef",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 概述\n",
    "\n",
    "Numpy - Numerical Python - C语言写的\n",
    "\n",
    "- 数值计算\n",
    "- 处理数据：多维/一维 数组\n",
    "- 线性代数计算\n",
    "- C语言API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebfc6b61",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 导入Numpy\n",
    "\n",
    "1. 标准导入Numpy的方式`import numpy as np`\n",
    "2. 想要省略我们之后np. 可以使用`from numpy import *`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3096e488",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 导入库\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5869bb11",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 数组的大小与类型\n",
    "1. 随机生成一个数组，查看大小和维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6dd79c82",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "data = np.random.randn(2,3)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ba225572",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,)\n"
     ]
    }
   ],
   "source": [
    "#查看数组的大小\n",
    "print(data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b2188c5c",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "#查看数组的维度\n",
    "print(data.ndim) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f34cc8f",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "2. 查看该数据的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b3a6d242",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float64\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(data.dtype) # 查看数组中的数据（数组内的值）类型\n",
    "print(type(data))# 查看data（该数组）本身的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "118176aa",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 生成一个数组\n",
    "1. 通过python原生数组生成一个numpy数组 - numpy的数组类型ndarrry与python数组类型list不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b2df8821",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "#通用转换python数组-> numpy数组\n",
    "data = [1,2,3,4,5]\n",
    "print(type(data))\n",
    "arr1 = np.array(data) # 转换语句\n",
    "print(type(arr1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67d55db3",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "> 多维数组也可以使用嵌套list序列进行转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1ec1fa0a",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "data = [[1,2,3,4,5],[6,7,8,9,10]] #新建一个二维数组\n",
    "arr1 = np.array(data) #转换语句\n",
    "print(arr1) # 打印数组\n",
    "print(type(arr1)) #打印数组类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdf9cc26",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "2. 通过numpy自带的函数来生成数组\n",
    "    - np.array(数据) #将数据转换为ndarray\n",
    "    - np.ones(shape, dtype=None) #将给定的形状和数据类型生成全都是1的数组 shape传入元组(2,3）或者数字3\n",
    "    - np.zeros(shape, dtype=None)#同上生成全都是0的数组\n",
    "    - np.empty(shape, dtype=None)#同上生成全都是没有初始化数值的空数组\n",
    "    - np.full(shape, fill_value, dtype=None)#同上生成指定数值的数组,fill_value代表这个指定的数值，可以是list列表/数组，或者是单个值\n",
    "    - np.eye(N,M) #生成一个特征矩阵，就是对角线位置都是1，其余位置都是0的数组,N代表行，M代表列，不指定M的话就生成N* N的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1c5c9387",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n",
      "[1 1 1 1 1 1 1 1 1 1 1]\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "#生成全1数组\n",
    "print(np.ones(11)) #生成一个有11个1的数组，默认是float64也就是小数\n",
    "print(np.ones(11,np.int64))#生成一个有11个1的数组，强制数组里面的1为整型（整数）\n",
    "print(np.ones((3,4)))# 生成三行四列的全1数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d654d8fb",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#生成全0数组\n",
    "print(np.zeros((3,4)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7c93e370",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#生成空数组\n",
    "print(np.empty((2,6)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4597fe79",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[99 99 99]\n",
      " [99 99 99]]\n",
      "[[1 2 3 4 5]\n",
      " [1 2 3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "# 生成指定数值的数组\n",
    "print(np.full((2,3),99)) #指定数值->单个值\n",
    "print(np.full((2,5),[1,2,3,4,5])) #指定数值->数组,要保证该数组能够填进指定大小的数组中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "56eade2d",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0.]\n",
      " [0. 0. 0. 0. 1.]]\n",
      "[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n",
      " [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#生成特征矩阵\n",
    "print(np.eye(5)) #生成5*5的矩阵\n",
    "print(np.eye(6,10))#生成6*10的矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30e546c8",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Numpy中的数据类型\n",
    "1. int,uint 有符号或者无符号的整数 int8,int16,int32,int64 后面的数字代表位数\n",
    "2. float 浮点数，小数 float16,float32,float64,float128\n",
    "3. complex 复数 complex64,complex128,complex256\n",
    "4. bool 布尔值 True,False\n",
    "5. object 类型\n",
    "6. str_ 字符串类型,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5697dac7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0 0]\n",
      " [0 1 0 0 0]\n",
      " [0 0 1 0 0]\n",
      " [0 0 0 1 0]\n",
      " [0 0 0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "data = np.eye(5)\n",
    "data = data.astype('int64')#data转换类型为int64类型，传入参数为字符串\n",
    "data = data.astype(np.int8)#data转换类型为int8类型，传入参数为numpy类型\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97132fd5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}